﻿using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;

namespace ManagementPlatform.Utillity.Autheorza
{
    public static class AuthenticationEXP
    {
        public static void AddAtuhentEXP(this WebApplicationBuilder builder)
        {
            //添加cookie策略型身份验证
            builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
          .AddCookie(options =>
          {
              options.ExpireTimeSpan = TimeSpan.FromMinutes(20);
              options.SlidingExpiration = true;
              options.LoginPath = "/Home/Login";//在鉴权时，如果用户没有登录，会跳转到这个地址
              options.AccessDeniedPath = "/Home/Index";//在授权之后，如果用户没有权限，会跳转到这个地址
          });
            //添加授权
            builder.Services.AddAuthorization(options =>
            {
                options.AddPolicy("PlatAuthoriPloicy", policy =>
                {
                    policy.AddAuthenticationSchemes(CookieAuthenticationDefaults.AuthenticationScheme);
                    policy.Requirements.Add(new AuthorizaRequirement());//通过这个类来调用自己创建的授权类
                });
             
            });
            //添加授权处理程序服务
            builder.Services.AddSingleton<IAuthorizationHandler, AuthorizaHandler>();
        }
    }
}